package cate.game.role.task;

import cate.common.table.d.GDTask;
import cate.common.table.task.row.TaskLibRow;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.history.HistoryItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

@NoteClass(value = "主角任务记录项")
public class TaskItem extends RoleDependent {
    @NoteField(value = "所属功能组的功能ID")
    public int funcId;

    @NoteField(value = "任务模板id")
    public int tid;

    @NoteField(value = "记录值")
    public long value;

    @NoteField(value = "任务已完成")
    public boolean done = false;

    @NoteField(value = "奖励是否已领取")
    public boolean rewardGot = false;


    public TaskItem() {}

    public TaskItem(Role role, int tid, int funcId) {
        super(role);
        this.tid = tid;
        this.funcId = funcId;
        if (getTpl() != null) {
            this.value = getTpl().valueInit;
        }
        initialize(role);
    }

    @Override
    public void initialize(Role role){
        super.initialize(role);
        check();
    }

    public void check() {
        if (!done) {
            TaskInitValue.handle(this);
            this.checkDone();
        }
    }

    /** 重置值（因为跨天或者跨周或者跨月等原因） */
    public void reset() {
        this.value = getTpl().valueInit;
        this.done = false;
        this.rewardGot = false;
    }

    @JsonIgnore
    public TaskLibRow getTpl() {
        if(role == null){
            logger.error("当场逮捕，taskId={},funcId={},value={}", tid, funcId, value);
            return null;
        }
        return role.getGame().table.task.lib.get(this.tid);
    }

    /** 检查任务是否已完成 */
    private void checkDone() {
        TaskLibRow tpl = getTpl();
        if (tpl == null) {
            logger.error("缺少模板ID={}的任务配置！", this.tid);
            return;
        }
        long checkValue = tpl.valueInit;
        if (tpl.duration == GDTask.Duration.PERMANENT) {
            HistoryItem his = role.getHistory().getItem(tpl.event, tpl.param);
            if (his == null) {
                return;
            }
            if (tpl.checkType == GDTask.CheckType.CUR) {
                checkValue = his.cur;
            } else if (tpl.checkType == GDTask.CheckType.MIN) {
                checkValue = his.min;
            } else if (tpl.checkType == GDTask.CheckType.MAX) {
                checkValue = his.max;
            }
            // 永久历史的任务的值可以直接从永久历史中取得。
            this.value = checkValue = Math.max(this.value, checkValue);
        } else {
            checkValue = this.value;
        }
        switch (tpl.checkOper) {
            case GDTask.CheckOper.EQ:
                this.done = checkValue == tpl.value;
                break;
            case GDTask.CheckOper.GTE:
                this.done = checkValue >= tpl.value;
                break;
            case GDTask.CheckOper.LTE:
                this.done = checkValue <= tpl.value;
                break;
            default:
                break;
        }
    }

    /**
     * 设置值
     *
     * @return 记录值是否改变
     */
    public boolean setValue(long v, boolean add) {
        long oldValue = this.value;
        boolean oldDone = this.done;
        this.value = add ? this.value + v : v;
        checkDone();
        return (this.value != oldValue || this.done != oldDone);
    }

    @Override
    public String toString() {
        return "(tid="+tid+",value="+value+",done="+done+",rewardGot="+rewardGot+")";
    }
}
